<?php if(!defined('BASEPATH'))exit('No direct script access allowed');

class Ads extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {
        $page = 1;
        if($this->uri->segment(4) != '')
        {
             $page = $this->uri->segment(4);
        }
        //select data for ads viewest
        $sql = "SELECT adsID,adsTitle,adsView FROM {PRE}ads WHERE adsView > 0 ORDER BY adsView DESC";
        $data['rowadsView'] = $this->global_model->fetch($sql);
        //select data for ads Vip
        $sql = "SELECT {PRE}ads.adsID,{PRE}ads.adsTitle,{PRE}ads.adsView,{PRE}ads.adsPostDate,{PRE}province.preName ";
        $sql .= "FROM {PRE}ads,{PRE}province ";
        $sql .= "WHERE {PRE}ads.adsVip > 0 AND {PRE}ads.adsProvince = {PRE}province.preID ";
        $sql .= "ORDER BY {PRE}ads.adsView DESC";
        $data['rowadsVip'] = $this->global_model->fetch($sql);
        //select data of ads from best shop
        $sql = "SELECT DISTINCT({PRE}ads.adsID),{PRE}ads.adsTitle,{PRE}ads.adsView ";
        $sql .= "FROM {PRE}ads,{PRE}product ";
        $sql .= "WHERE {PRE}ads.adsUser = {PRE}product.proUser AND {PRE}product.proBestShop > 0 ";
        $sql .= "ORDER BY {PRE}ads.adsView DESC";
        $data['rowadsBestShop'] = $this->global_model->fetch($sql);
        //get infomation for pagination ads New
        $sql = "SELECT adsID FROM {PRE}ads WHERE adsExpireDate = 0";
        $result = $this->global_model->fetch($sql);
        $totalrows = count($result);
        $numpages = ceil($totalrows/3);
        //select data of ads New
        $sql = "SELECT {PRE}ads.adsID,{PRE}ads.adsTitle,{PRE}ads.adsView,{PRE}ads.adsPostDate,{PRE}province.preName ";
        $sql .= "FROM {PRE}ads,{PRE}province ";
        $sql .= "WHERE adsExpireDate = 0 AND {PRE}ads.adsProvince = {PRE}province.preID ";
        $sql .= "ORDER BY {PRE}ads.adsView DESC limit ".(($page-1)*3).", 3";
        $data['rowadsNew'] = $this->global_model->fetch($sql);
        $data['numpages'] = $numpages;
        $data['currentpage'] = $page;
        
        $this->load->view('home/ads/index',$data);
    }
    
    function ads_detail()
    {
        $adsId = $this->uri->segment(4);
        $pageComment = $this->uri->segment(5);
        
        #if exists comment data from client, process
        $title = $this->input->post('txtTitle');
        $content = '';
        if(isset($_POST['txtDetail']))
        {
            $content = $_POST['txtDetail'];    
        }
        $codeConfirm = $this->input->post('txtCaptcha');
        if($title != '' && $content != '' && $codeConfirm != '')
        {
            $sql = "SELECT adsUser FROM {PRE}ads WHERE adsID = $adsId";
            $result = $this->global_model->fetch($sql);
            $sql = "INSERT INTO {PRE}ads_comment(adcUser,adcAds,adcTitle,adcDetail,adcStatus,adcPostDate) VALUES(?,?,?,?,?,?)";
            $this->db->query($sql, array($result[0]->adsUser,$adsId,$title,$content,1,time()));
        }
        #end process comment

        //sslect ads of the same member
        $adsUser = $this->ads_model->get_ads_User($adsId);
        $sql = "SELECT adsID,adsTitle,adsView ";
        $sql .= "FROM {PRE}ads ";
        $sql .= "WHERE adsUser = $adsUser ";
        $sql .= "ORDER BY adsView DESC";
        $data['rowadsSameMember'] = $this->global_model->fetch($sql);
        
        //select ads of the same section
        $adsSection=$this->ads_model->get_ads_Section($adsId);
        $sql = "SELECT adsID,adsTitle,adsView ";
        $sql .= "FROM {PRE}ads ";
        $sql .= "WHERE adsSection = $adsSection ";
        $sql .= "ORDER BY adsView DESC";
        $data['rowadsSameSection']=$this->global_model->fetch($sql);
        
        //update view of ads detail
        if($adsId > 0 && $pageComment == '')
        {
          $this->ads_model->update_ads_view($adsId);   
        }
        //select data for display the detail ads
        $sql = "SELECT * FROM {PRE}ads,{PRE}province,{PRE}user ";
        $sql.= "WHERE {PRE}ads.adsID = $adsId AND {PRE}ads.adsProvince = {PRE}province.preID AND {PRE}ads.adsUser = {PRE}user.useID";
        $data['rowadsDetail'] = $this->global_model->fetch($sql);
        
        //select comment of ads detail, process paging for ads comment
        $sql = "SELECT * FROM {PRE}ads_comment WHERE adcAds = $adsId";
        $result = $this->global_model->fetch($sql);
        $numrows = count($result);
        $numpageComment = ceil($numrows/2);
        if($pageComment == 0)
        {
            $pageComment = 1;
        }
        $data['currentpageComment'] = $pageComment;
        $data['numpageComment'] = $numpageComment;
        $data['adsID'] = $adsId;
        
        $sql = "SELECT * FROM {PRE}ads_comment WHERE adcAds = $adsId limit ".(($pageComment-1)*2)." ,2";
        $data['rowadsComment'] = $this->global_model->fetch($sql);
        
        $this->load->view("home/ads/ads_detail", $data);
    }
    
    function ads_search()
    {
        #load data for control form search
        $sql = "SELECT preID,preName FROM {PRE}province";
        $data['rowProvince'] = $this->global_model->fetch($sql);
        $sql = "SELECT secID,secName FROM {PRE}section";
        $data['rowSection'] = $this->global_model->fetch($sql);
        $sql = "SELECT catID,catName FROM {PRE}category";
        $data['rowCategory'] = $this->global_model->fetch($sql);
        #end load
        //load ads trom best shop
        $sql = "SELECT DISTINCT({PRE}ads.adsID),{PRE}ads.adsTitle,{PRE}ads.adsView ";
        $sql .= "FROM {PRE}ads,{PRE}product ";
        $sql .= "WHERE {PRE}ads.adsUser = {PRE}product.proUser AND {PRE}product.proBestShop > 0 ";
        $sql .= "ORDER BY {PRE}ads.adsView DESC";
        $data['rowadsBestShop'] = $this->global_model->fetch($sql);
        //load ads viewest
        //select data for ads viewest
        $sql = "SELECT adsID,adsTitle,adsView FROM {PRE}ads WHERE adsView > 0 ORDER BY adsView DESC";
        $data['rowadsView'] = $this->global_model->fetch($sql);
        #get criteria from client
        $title = $this->input->post('txtTitle');
        $adsVip = '';
        $adsFromView = $this->input->post('txtFromView');
        $adsToView = $this->input->post('txtToView');
        $adsProvince = '';
        $adsSection = '';
        $adsCategory = '';
        $adsFromDate = $this->input->post('txtFromDate');
        $adsToDate = $this->input->post('txtToDate');
        if(isset($_POST['txtVip']))
        {
            $adsVip = $_POST['txtVip'];
        }
        if(isset($_POST['txtProvince']))
        {
            $adsProvince = $_POST['txtProvince'];
        }
        if(isset($_POST['txtSection']))
        {
            $adsSection = $_POST['txtSection'];
        }
        if(isset($_POST['txtCategory']))
        {
            $adsCategory = $_POST['txtCategory'];
        }
        #end criteria
        //get current page
        $page = 1;
        if($this->uri->segment(4) != '')
        {
            $page = $this->uri->segment(4);
        }
        # create sql query
        $sql = "SELECT {PRE}ads.adsID,{PRE}ads.adsTitle,{PRE}ads.adsPostDate,{PRE}ads.adsView,{PRE}province.preName ";
        $sql .= "FROM {PRE}ads, {PRE}province ";
        $sql .= "WHERE {PRE}ads.adsProvince = {PRE}province.preID";
        if($title != '')
        {
            $sql .= " AND {PRE}ads.adsTitle like '%".$title."%'";    
        }
        if($adsVip != '')
        {
            $sql .= " AND {PRE}ads.adsVip > 0";
        }
        if($adsFromView > 0)
        {
            $sql .= " AND {PRE}ads.adsView >= $adsFromView";
        }
        if($adsToView > 0)
        {
            $sql .= " AND {PRE}ads.adsView <= $adsToView";
        }
        if($adsProvince != '')
        {
            $sql .= " AND {PRE}ads.adsProvince = $adsProvince";
        }
        if($adsSection != '')
        {
            $sql .= " AND {PRE}ads.adsSection = $adsSection";
        }
        if($adsCategory != '')
        {
            $sql .= " AND {PRE}ads.adsCategory = $adsCategory";
        }
        if($adsFromDate != '')
        {
            $sql .= " AND {PRE}ads.adsPostDate >= $adsFromDate";
        }
        if($adsToDate != '' )
        {
            $sql .= " AND {PRE}ads.adsPostDate <= $adsToDate";
        }
        
        $data['numpages'] = ceil(count($this->global_model->fetch($sql))/5);
        $data['totalrows'] = count($this->global_model->fetch($sql));
        $data['currentpage'] = $page;
        
        $sql .= " ORDER BY {PRE}ads.adsView DESC"; 
        $sql .= " limit ".(($page-1)*5).",5";
       
        
        $data['rowAds'] = $this->global_model->fetch($sql);
        
        $this->load->view("home/ads/ads_search", $data);
    }
    
    function ads_favorite()
    {
        //get ads ID
        $adsId = $this->uri->segment(4);
        //check ads favorite
        if($adsId != '')
        {
            if(!$this->ads_model->check_ads_favorite($adsId))
            {
                //select an information of ads 
                $sql = "SELECT adsUser,adsPostDate FROM {PRE}ads WHERE adsID = $adsId";
                $result = $this->global_model->fetch($sql);
                if(count($result) > 0)
                {
                    //add ads bad
                    $this->ads_model->add_ads_favorite($adsId,$result[0]->adsUser,$result[0]->adsPostDate);
                }
            }
        }
        redirect(site_url('home/ads/ads_detail/')."/".$adsId."/fvor");
    }
    
    function ads_bad()
    {
        //get ads ID
        $adsId = $this->uri->segment(4);
        //check ads bad
        if($adsId != '')
        {
            if(!$this->ads_model->check_ads_bad($adsId))
            {
                //select an information of ads 
                $sql = "SELECT adsEmail,adsTitle,adsDetail,adsPostDate FROM {PRE}ads WHERE adsID = $adsId";
                $result = $this->global_model->fetch($sql);
                if(count($result) > 0)
                {
                    //add ads bad
                    $this->ads_model->add_ads_bad($adsId,$result[0]->adsEmail,$result[0]->adsTitle,$result[0]->adsDetail,$result[0]->adsPostDate);
                }
            }
        }
        redirect(site_url('home/ads/ads_detail/')."/".$adsId."/bad");
    }
    
    function member()
    {
        $this->load->view("home/ads/member");
    }
}